package com.lh.controller;

import com.lh.entity.Goods;
import com.lh.entity.GoodsType;
import com.lh.entity.GoodsVo;
import com.lh.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;


    @RequestMapping("toshow")
    public String findAll(HttpServletRequest request, GoodsVo goodsVo) {

        List<GoodsVo> ulist = goodsService.findAll(goodsVo);
        request.setAttribute("ulist", ulist);
        List<GoodsType> gtlist = goodsService.findAllGoodsType();
        request.setAttribute("gtlist", gtlist);
        return "welcome";
    }


    @RequestMapping("toadd")
    public String toadd(HttpServletRequest request) {
        //查询下拉列表数据


        //     List<Grade> glsit = userService.findAllGrade();
        //   List<Goods> clist = userService.findAllCourse();
        List<GoodsType> gtlist = goodsService.findAllGoodsType();
        request.setAttribute("gtlist", gtlist);
        return "add";
    }

    @RequestMapping("add")
    public String add(Goods goods) {
        //  userService.add(u);
        goodsService.add(goods);

        return "redirect:/goods/toshow";
    }

    @RequestMapping("toupdate")
    public String toupdate(HttpServletRequest request, Integer id) {
        Goods goods = goodsService.findUserById(id);
        request.setAttribute("goods", goods);
        List<GoodsType> gtlist = goodsService.findAllGoodsType();
        request.setAttribute("gtlist", gtlist);
        return "update";
    }

    @RequestMapping("update")
    public String update(Goods goods) {
        goodsService.update(goods);
        return "redirect:/goods/toshow";
    }

    @RequestMapping("todel")
    public String todel(Integer id) {
        goodsService.todel(id);
        return "redirect:/goods/toshow";
    }
}
